<template>
  <view class="container">
    <!-- 加载状态 -->
    <view class="loading-container" v-if="loading">
      <image src="/static/loading.png" class="loading-image"></image>
      <text class="loading-text">加载中...</text>
    </view>

    <!-- 错误状态 -->
    <view class="error-container" v-else-if="error">
      <image src="/static/error.png" class="error-image"></image>
      <text class="error-text">{{errorMsg}}</text>
      <button class="retry-btn" @click="loadFoodDetail">重试</button>
    </view>

    <!-- 内容区域 -->
    <block v-else>
      <!-- 头部图片 -->
      <swiper class="food-header-swiper" indicator-dots circular autoplay interval="4000" duration="500">
        <swiper-item v-for="(image, index) in food.images" :key="index">
          <image class="food-cover" :src="image" mode="aspectFill"></image>
        </swiper-item>
      </swiper>

      <!-- 基本信息 -->
      <view class="food-basic-info">
        <text class="food-name">{{food.name}}</text>
        <view class="food-tags">
          <text class="tag" v-for="(tag, index) in food.tags" :key="index">{{tag}}</text>
        </view>
        <view class="food-score-price">
          <text class="food-score">{{food.rating}}分</text>
          <text class="food-price">人均 ¥{{food.avgPrice}}</text>
        </view>

        <!-- 地址和营业时间 -->
        <view class="info-row">
          <image src="/static/location.png" class="info-icon"></image>
          <text class="info-text">{{food.address}}</text>
          <button class="map-btn" size="mini" @click="openMap">导航</button>
        </view>
        
        <view class="info-row">
          <image src="/static/time.png" class="info-icon"></image>
          <text class="info-text">{{food.openHours}}</text>
        </view>
        
        <view class="info-row">
          <image src="/static/phone.png" class="info-icon"></image>
          <text class="info-text">{{food.phone}}</text>
          <button class="call-btn" size="mini" @click="makePhoneCall">拨打</button>
        </view>
      </view>

      <!-- 操作按钮 -->
      <view class="action-row">
        <view class="action-btn" @click="toggleLike">
          <image :src="food.isLiked ? '/static/liked.png' : '/static/like.png'" class="action-icon"></image>
          <text class="action-text">{{food.likeCount}} 赞</text>
        </view>
        <view class="action-btn" @click="shareFood">
          <image src="/static/share.png" class="action-icon"></image>
          <text class="action-text">分享</text>
        </view>
        <view class="action-btn" @click="collectFood">
          <image :src="food.isCollected ? '/static/collected.png' : '/static/collect.png'" class="action-icon"></image>
          <text class="action-text">收藏</text>
        </view>
      </view>

      <!-- 详细描述 -->
      <view class="food-description">
        <view class="section-title">
          <text>餐厅简介</text>
        </view>
        <text class="desc-text">{{food.description}}</text>
      </view>

      <!-- 特色菜品 -->
      <view class="food-dishes">
        <view class="section-title">
          <text>特色菜品</text>
        </view>
        <scroll-view scroll-x class="dishes-scroll">
          <view class="dish-item" v-for="(dish, index) in food.dishes" :key="index">
            <image :src="dish.image" class="dish-image" mode="aspectFill"></image>
            <text class="dish-name">{{dish.name}}</text>
            <text class="dish-price">¥{{dish.price}}</text>
          </view>
        </scroll-view>
      </view>

      <!-- 用户评论 -->
      <view class="food-comments">
        <view class="section-title">
          <text>用户评论 ({{food.comments.length}})</text>
        </view>
        <view class="comment-item" v-for="(comment, index) in food.comments" :key="index">
          <image :src="comment.userAvatar" class="comment-avatar"></image>
          <view class="comment-content">
            <view class="comment-user-row">
              <text class="comment-username">{{comment.userName}}</text>
              <text class="comment-time">{{comment.time}}</text>
            </view>
            <text class="comment-text">{{comment.content}}</text>
            <view class="comment-images" v-if="comment.images && comment.images.length > 0">
              <image 
                v-for="(img, imgIndex) in comment.images" 
                :key="imgIndex" 
                :src="img" 
                class="comment-image" 
                mode="aspectFill"
                @click="previewImage(comment.images, imgIndex)"></image>
            </view>
          </view>
        </view>
        
        <!-- 没有评论的提示 -->
        <view class="no-comments" v-if="food.comments.length === 0">
          <text>暂无评论，快来发表第一条评论吧！</text>
        </view>
      </view>

      <!-- 底部评论栏 -->
      <view class="comment-bar">
        <input 
          type="text" 
          v-model="commentText" 
          placeholder="写下你的评价..." 
          confirm-type="send"
          @confirm="submitComment"
        />
        <button class="submit-btn" @click="submitComment">发送</button>
      </view>
    </block>
  </view>
</template>

<script>
export default {
  data() {
    return {
      foodId: '',
      loading: true,
      error: false,
      errorMsg: '加载失败，请重试',
      commentText: '',
      food: {
        id: '1',
        name: '阿强海鲜大排档',
        images: ['/static/图片.jpg', '/static/图片.jpg', '/static/图片.jpg'],
        tags: ['海鲜', '螃蟹', '生蚝'],
        rating: 4.8,
        avgPrice: 128,
        address: '沙扒湾海滩路128号',
        openHours: '10:00-22:00',
        phone: '0662-12345678',
        type: 'seafood',
        features: ['新鲜', '特色调料', '海景餐厅'],
        isLiked: false,
        likeCount: 235,
        isCollected: false,
        description: '阿强海鲜大排档是沙扒湾最著名的海鲜餐厅之一，位于沙扒湾海滩旁边，拥有绝佳的海景视野。餐厅主打新鲜海鲜，每天从当地渔民手中直接采购最新鲜的海产品。店主阿强有着20多年的海鲜烹饪经验，擅长粤式和潮汕风味的海鲜烹饪。餐厅的招牌菜有清蒸海鲜拼盘、椒盐濑尿虾、避风塘炒蟹等。餐厅环境简约但干净整洁，是品尝正宗海鲜的好去处。',
        dishes: [
          { name: '清蒸鲈鱼', price: 88, image: '/static/图片.jpg' },
          { name: '椒盐濑尿虾', price: 136, image: '/static/图片.jpg' },
          { name: '避风塘炒蟹', price: 168, image: '/static/图片.jpg' },
          { name: '蒜蓉蒸生蚝', price: 98, image: '/static/图片.jpg' },
          { name: '清蒸海鲜拼盘', price: 228, image: '/static/图片.jpg' }
        ],
        comments: [
          {
            id: 'c1',
            userName: '沙滩爱好者',
            userAvatar: '/static/avatar.png',
            content: '海鲜非常新鲜，店家的做法也很地道，蒜蓉蒸生蚝特别推荐，非常美味！',
            time: '2023-08-15',
            images: ['/static/图片.jpg', '/static/图片.jpg']
          },
          {
            id: 'c2',
            userName: '美食探索者',
            userAvatar: '/static/avatar.png',
            content: '环境不错，可以看到海景，海鲜种类也挺多的，价格在当地算是中等水平。服务态度很好。',
            time: '2023-08-10',
            images: []
          },
          {
            id: 'c3',
            userName: '打卡达人',
            userAvatar: '/static/avatar.png',
            content: '位置很好找，就在海滩旁边，晚上吃饭还能看到星星。清蒸的海鲜最能保持原味，推荐！',
            time: '2023-08-05',
            images: ['/static/图片.jpg']
          }
        ]
      }
    }
  },
  onLoad(options) {
    if (options.id) {
      this.foodId = options.id;
      this.loadFoodDetail();
    }
    // 设置分享参数
    uni.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    });
  },
  methods: {
    // 加载餐厅详情
    loadFoodDetail() {
      this.loading = true;
      this.error = false;
      
      // 模拟加载
      setTimeout(() => {
        // 实际项目中，这里应该发起请求获取数据
        // 例如: this.fetchFoodDetail(this.foodId)
        
        // 示例中使用静态数据
        this.loading = false;
      }, 800);
    },
    
    // 切换点赞
    toggleLike() {
      if (!this.checkUserLogin()) return;
      
      this.food.isLiked = !this.food.isLiked;
      if (this.food.isLiked) {
        this.food.likeCount++;
      } else {
        this.food.likeCount--;
      }
      
      // 实际项目中，这里应该发起请求更新数据
      // 例如: this.updateFoodLike(this.foodId, this.food.isLiked)
    },
    
    // 收藏餐厅
    collectFood() {
      if (!this.checkUserLogin()) return;
      
      this.food.isCollected = !this.food.isCollected;
      uni.showToast({
        title: this.food.isCollected ? '收藏成功' : '已取消收藏',
        icon: 'none'
      });
      
      // 实际项目中，这里应该发起请求更新数据
      // 例如: this.updateFoodCollection(this.foodId, this.food.isCollected)
    },
    
    // 分享餐厅
    shareFood() {
      // 小程序中的分享逻辑由用户点击右上角菜单触发
      uni.showToast({
        title: '点击右上角分享给好友',
        icon: 'none'
      });
    },
    
    // 打开地图导航
    openMap() {
      uni.openLocation({
        latitude: 21.5846,  // 这里使用虚构的经纬度，实际项目中应使用真实数据
        longitude: 111.8627,
        name: this.food.name,
        address: this.food.address,
        scale: 18
      });
    },
    
    // 拨打电话
    makePhoneCall() {
      uni.makePhoneCall({
        phoneNumber: this.food.phone
      });
    },
    
    // 预览图片
    previewImage(images, current) {
      uni.previewImage({
        urls: images,
        current: images[current]
      });
    },
    
    // 提交评论
    submitComment() {
      if (!this.checkUserLogin()) return;
      
      if (!this.commentText.trim()) {
        uni.showToast({
          title: '评论内容不能为空',
          icon: 'none'
        });
        return;
      }
      
      // 添加评论到本地数据
      const newComment = {
        id: 'c' + Date.now(),
        userName: '当前用户',
        userAvatar: '/static/avatar.png',
        content: this.commentText,
        time: new Date().toISOString().split('T')[0],
        images: []
      };
      
      this.food.comments.unshift(newComment);
      this.commentText = '';
      
      uni.showToast({
        title: '评论成功',
        icon: 'success'
      });
      
      // 实际项目中，这里应该发起请求提交评论
      // 例如: this.postComment(this.foodId, this.commentText)
    },
    
    // 检查用户是否登录
    checkUserLogin() {
      const userInfo = uni.getStorageSync('userInfo');
      if (!userInfo) {
        uni.showModal({
          title: '提示',
          content: '请先登录',
          success: (res) => {
            if (res.confirm) {
              uni.navigateTo({
                url: '/pages/login/index'
              });
            }
          }
        });
        return false;
      }
      return true;
    },
    
    // 分享到朋友圈或好友
    onShareAppMessage() {
      return {
        title: this.food.name + ' - 沙扒湾特色美食',
        path: '/pages/foods/detail?id=' + this.foodId,
        imageUrl: this.food.images[0]
      };
    },
    
    onShareTimeline() {
      return {
        title: this.food.name + ' - 沙扒湾特色美食',
        query: 'id=' + this.foodId,
        imageUrl: this.food.images[0]
      };
    }
  }
}
</script>

<style>
.container {
  padding-bottom: 120rpx;
}

.loading-container, .error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

.loading-image, .error-image {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 20rpx;
}

.loading-text, .error-text {
  font-size: 28rpx;
  color: #999;
  margin-bottom: 20rpx;
}

.retry-btn {
  margin-top: 20rpx;
  background: #f8f8f8;
  border: 1px solid #ddd;
  color: #333;
  font-size: 28rpx;
  padding: 10rpx 30rpx;
  border-radius: 30rpx;
}

.food-header-swiper {
  width: 100%;
  height: 400rpx;
}

.food-cover {
  width: 100%;
  height: 100%;
}

.food-basic-info {
  padding: 30rpx;
  background-color: #fff;
  border-radius: 20rpx 20rpx 0 0;
  margin-top: -20rpx;
  position: relative;
  z-index: 1;
}

.food-name {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 15rpx;
}

.food-tags {
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 15rpx;
}

.tag {
  font-size: 24rpx;
  color: #ff6b6b;
  background-color: #fff5f5;
  padding: 8rpx 15rpx;
  border-radius: 6rpx;
  margin-right: 15rpx;
  margin-bottom: 10rpx;
}

.food-score-price {
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;
}

.food-score {
  font-size: 32rpx;
  color: #ff9900;
  font-weight: bold;
  margin-right: 30rpx;
}

.food-price {
  font-size: 28rpx;
  color: #666;
}

.info-row {
  display: flex;
  align-items: center;
  margin-bottom: 20rpx;
}

.info-icon {
  width: 36rpx;
  height: 36rpx;
  margin-right: 15rpx;
}

.info-text {
  flex: 1;
  font-size: 28rpx;
  color: #666;
}

.map-btn, .call-btn {
  background: #f0f0f0;
  color: #666;
  font-size: 24rpx;
  padding: 0 20rpx;
  height: 50rpx;
  line-height: 50rpx;
  margin-left: 15rpx;
}

.action-row {
  display: flex;
  justify-content: space-around;
  padding: 30rpx;
  background-color: #fff;
  margin-top: 20rpx;
  border-radius: 12rpx;
}

.action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.action-icon {
  width: 48rpx;
  height: 48rpx;
  margin-bottom: 10rpx;
}

.action-text {
  font-size: 24rpx;
  color: #666;
}

.food-description, .food-dishes, .food-comments {
  margin-top: 20rpx;
  padding: 30rpx;
  background-color: #fff;
  border-radius: 12rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  border-left: 8rpx solid #ff6b6b;
  padding-left: 15rpx;
  margin-bottom: 20rpx;
}

.desc-text {
  font-size: 28rpx;
  color: #666;
  line-height: 1.6;
}

.dishes-scroll {
  white-space: nowrap;
  margin: 0 -30rpx;
  padding: 0 30rpx;
}

.dish-item {
  display: inline-block;
  width: 220rpx;
  margin-right: 20rpx;
}

.dish-image {
  width: 220rpx;
  height: 220rpx;
  border-radius: 8rpx;
  margin-bottom: 10rpx;
}

.dish-name {
  font-size: 26rpx;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

.dish-price {
  font-size: 24rpx;
  color: #ff6b6b;
}

.comment-item {
  display: flex;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #eee;
}

.comment-avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  margin-right: 20rpx;
}

.comment-content {
  flex: 1;
}

.comment-user-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10rpx;
}

.comment-username {
  font-size: 28rpx;
  color: #333;
  font-weight: bold;
}

.comment-time {
  font-size: 24rpx;
  color: #999;
}

.comment-text {
  font-size: 28rpx;
  color: #666;
  line-height: 1.5;
  margin-bottom: 15rpx;
}

.comment-images {
  display: flex;
  flex-wrap: wrap;
}

.comment-image {
  width: 150rpx;
  height: 150rpx;
  margin-right: 10rpx;
  margin-bottom: 10rpx;
  border-radius: 8rpx;
}

.no-comments {
  padding: 30rpx 0;
  text-align: center;
  color: #999;
  font-size: 28rpx;
}

.comment-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  display: flex;
  padding: 20rpx 30rpx;
  background-color: #fff;
  border-top: 1rpx solid #eee;
  box-sizing: border-box;
}

.comment-bar input {
  flex: 1;
  height: 70rpx;
  background-color: #f8f8f8;
  border-radius: 35rpx;
  padding: 0 30rpx;
  font-size: 28rpx;
}

.submit-btn {
  width: 120rpx;
  height: 70rpx;
  line-height: 70rpx;
  background-color: #ff6b6b;
  color: #fff;
  font-size: 28rpx;
  margin-left: 20rpx;
  border-radius: 35rpx;
}
</style> 